//
//  UIButton+Extension.swift
//  SinaWeibo
//
//  Created by Tony-ztb on 15/12/18.
//  Copyright © 2015年 Tony-ztb. All rights reserved.
//

import UIKit


extension UIButton {
    
    // 便利的构造函数  基于本类的构造函数扩展
    convenience init(backImageName: String?, title: String, fontSize: CGFloat, color: UIColor, imageName: String? = nil) {
        self.init()
        if backImageName != nil {
            setBackgroundImage(UIImage(named: backImageName!), forState: .Normal)
        }
        if imageName != nil {
            setImage(UIImage(named: imageName!), forState: .Normal)
        }
        setTitle(title, forState: .Normal)
        titleLabel?.font = UIFont.systemFontOfSize(fontSize)
        setTitleColor(color, forState: .Normal)
        sizeToFit()
        
    }
    
    
    convenience init(imageName: String? = nil, backImageName: String) {
        self.init()
        if imageName != nil {
            setImage(UIImage(named: imageName!), forState: .Normal)
            setImage(UIImage(named: imageName! + "_highlighted"), forState: .Highlighted)
        }
        setBackgroundImage(UIImage(named: backImageName), forState: .Normal)
        setBackgroundImage(UIImage(named: backImageName + "_highlighted"), forState: .Highlighted)
        // 根据图片视图自动设置大小
        sizeToFit()
        
    }
}
